package br.com.struts.controller.cliente.dao;

import java.util.Collection;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import br.com.struts.model.Cliente;

@Repository
public class ClienteDAOImpl extends HibernateDaoSupport implements ClienteDAO {
	
	@Autowired
	public void getSessionFactoty(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	public Collection<Cliente> listarTodos() {
		Collection<Cliente> clientes = null;
		try {
			clientes = getHibernateTemplate().loadAll(Cliente.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clientes;
	}

	public Cliente buscarPorId(Long id) {
		Cliente cliente = null;
		try {
			cliente = getHibernateTemplate().get(Cliente.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cliente;
	}

	public void salvar(Cliente cliente) {
		try {
			getHibernateTemplate().save(cliente);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void atualizar(Cliente cliente) {
		try {
			getHibernateTemplate().merge(cliente);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void deletar(Cliente cliente) {
		try {
			getHibernateTemplate().delete(cliente);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
